package com.cw.lang.redis.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.util.CollectionUtils;

import com.cw.lang.redis.service.RedisForValueService;

import lombok.Getter;
import lombok.Setter;

/**
 * redisTemplate.opsForValue()操作字符串
 *
 * @author chenw
 * @version $$Id: RedisForValueServiceImpl.java, v 0.1 2018/11/19 3:29 PM chenw Exp $$
 */
@Getter
@Setter
public class RedisForValueServiceImpl<K extends Serializable, V extends Serializable> extends AbstractRedisService<K, V>
    implements RedisForValueService<K, V> {

    @Override
    public void set(K key, V value) {
        getRedisTemplate(key).opsForValue()
            .set(key, value);
    }

    @Override
    public void set(K key, V value, long timeout, TimeUnit unit) {
        getRedisTemplate(key).opsForValue()
            .set(key, value, timeout, unit);
    }

    @Override
    public Boolean setIfAbsent(K key, V value) {
        return getRedisTemplate(key).opsForValue()
            .setIfAbsent(key, value);
    }

    @Override
    public void multiSet(Map<? extends K, ? extends V> map) {
        if (CollectionUtils.isEmpty(map)) {
            return;
        }
        getRedisTemplate(map.keySet()
            .iterator()
            .next()).opsForValue()
            .multiSet(map);
    }

    @Override
    public Boolean multiSetIfAbsent(Map<? extends K, ? extends V> map) {
        if (CollectionUtils.isEmpty(map)) {
            return false;
        }
        return getRedisTemplate(map.keySet()
            .iterator()
            .next()).opsForValue()
            .multiSetIfAbsent(map);
    }

    @Override
    public V get(Object key) {
        return getRedisTemplate(key.getClass()).opsForValue()
            .get(key);
    }

    @Override
    public V getAndSet(K key, V value) {
        return getRedisTemplate(key).opsForValue()
            .getAndSet(key, value);
    }

    @Override
    public List<V> multiGet(Collection<K> keys) {
        if (CollectionUtils.isEmpty(keys)) {
            return new ArrayList<>();
        } else {
            return getRedisTemplate(keys.iterator()
                .next()).opsForValue()
                .multiGet(keys);
        }
    }

    @Override
    public Long increment(K key, long delta) {
        return getRedisTemplate(key).opsForValue()
            .increment(key, delta);
    }

    @Override
    public Double increment(K key, double delta) {
        return getRedisTemplate(key).opsForValue()
            .increment(key, delta);
    }

    @Override
    public Integer append(K key, String value) {
        return getRedisTemplate(key).opsForValue()
            .append(key, value);
    }

    @Override
    public String get(K key, long start, long end) {
        return getRedisTemplate(key).opsForValue()
            .get(key, start, end);
    }

    @Override
    public void set(K key, V value, long offset) {
        getRedisTemplate(key).opsForValue()
            .set(key, value, offset);
    }

    @Override
    public Long size(K key) {
        return getRedisTemplate(key).opsForValue()
            .size(key);
    }

    @Override
    public Boolean setBit(K key, long offset, boolean value) {
        return getRedisTemplate(key).opsForValue()
            .setBit(key, offset, value);
    }

    @Override
    public Boolean getBit(K key, long offset) {
        return getRedisTemplate(key).opsForValue()
            .getBit(key, offset);
    }
}
